<template>
  <el-dialog :title="'测评票预览'" :fullscreen='true' :close-on-click-modal="false" :visible.sync="show" width="100%">
<div class="fontDa">
    <div class="customTableHeader">
      <div class="customTableTitle">{{ cppName}}</div>
    </div>
    <div class="cpTable">
      <el-table :data="tableData" :span-method="objectSpanMethod" border height="calc(100vh - 270px)" :row-class-name="tableRowClassName">
        <el-table-column prop="zbName1" label="一级指标" align="center" min-width="300">
          <template slot-scope="scope">
            <!-- {{ scope.row.zbName1 }} -->
            <span v-if="scope.row.zbName3 || scope.row.zbName2"> {{ scope.row.zbName1 }}</span>
            <el-popover v-else placement="top-start" :title="scope.row.zbName1" width="700"
                        trigger="hover" :content="decodeURIComponent(decodeURIComponent(scope.row.tsNr))" popper-class="newline">
              <el-link slot="reference" type="primary" :underline="false"
                       style="margin-left:5px;font-size:24px !important">
                {{ scope.row.zbName1 }}
              </el-link>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="zbName2" label="二级指标" align="center" min-width="280" v-if="zbCj>1">
          <template slot-scope="scope">
            <el-popover placement="top-start" :title="scope.row.zbName2" width="700"
                        trigger="hover" :content="decodeURIComponent(decodeURIComponent(scope.row.tsNr))" popper-class="newline" :placement="shuxing">
              <el-link slot="reference" type="primary" :underline="false"
                       style="margin-left:5px;font-size:24px !important">
                {{ scope.row.zbName2 }}
              </el-link>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="zbName3" label="三级指标" align="center" min-width="280" v-if="zbCj>2">
          <template slot-scope="scope">
            <el-popover placement="top-start" :title="scope.row.zbName3" width="700"
                        trigger="hover" :content="decodeURIComponent(decodeURIComponent(scope.row.tsNr))"  popper-class="newline"  :placement="shuxing">
              <el-link slot="reference" type="primary" :underline="false"
                       style="margin-left:5px;font-size:24px !important">
                {{ scope.row.zbName3 }}
              </el-link>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column v-for="(h,i) in tableHeader" :key="i" :label="h" align="center">
          <el-table-column v-for="(gz,j) in tableColumn" :key="j+10000" :label="gz.fzMc" v-if="gz.fzSsz==h"
                           align="center" :fzIndex="j">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-success" class="cpCheck" v-if="scope.row.fzId==gz.id"></el-button>
              <el-button type="text" class="cpCheck" v-else  ><i class="noChecked"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
      <div style="width: 100%;" class="zfheaderbox" v-if="cepingDjmxbList!=null&&cepingDjmxbList.length>0">
        <div class="kaohe-firsttitle">评价等次：</div>
        <div class="flex customRadioNewBox">
          <label :class="'flex '+getClass(i,cepingDjmxbList.length)+'box footitem'" v-for="(d,i) in cepingDjmxbList"
                 :key="i" :for="i">
            <div :class="getClass(i,cepingDjmxbList.length)">{{ d.dcName }}</div>
            <div class="customRadioNew">
              <input :id="i" type="radio" name="pjdj" :value="d.id"
                     :checked="cepingDjmxb.dcId==d.id">
              <label :for="i"></label>
            </div>
          </label>
        </div>
      </div>
    </div>
</div>
  </el-dialog>
</template>

<script>
import {xpInfo, daFenCp, daFenDj} from "@/api/ceping/cepinghdglxpxxb";

export default {
  name: "cpIndexXp",
  data() {
    return {
      shuxing:"top-end",
      show: false,
      df: true,
      cppName:'',
      cepingHdglXpxxb: {},
      cepingHdglb: {},
      cepingHdglXpxxDfjlbList: [],
      cepingXmmxbList: [],
      cepingGzmxbList: [],
      cepingCpgzBcpmxb: {},
      cepingCpgzCpmxb: {},
      cepingCpgzCpmxCppzb: {},
      cepingDjmxbList: [],
      cepingCpgzBcpmxbUser: {},
      cepingCpgzBcpmxbDept: {},
      cepingXmmxbObj: {},
      zbCj: 2,//指标层级
      tableColumn: [],
      fzbList:[],
      tableHeader: [],
      tableData: [],//指标结构
      cepingDjmxb: {},
      ksdialogVisible: false
    }
  },
  methods: {
    initConfig(id) {
      this.show = false;
      this.df = true;

    },
    initXp(r) {
      this.tableData=[];
      this.show = false;
      this.df = true;
      this.$nextTick(()=>{
          this.cppName = r.cppName;
         //选票信息
        // this.cepingHdglXpxxb = res.data.data.cepingHdglXpxxb;
        //会议信息
        // this.cepingHdglb = res.data.data.cepingHdglb;
        //打分列表
        // this.cepingHdglXpxxDfjlbList = res.data.data.cepingHdglXpxxDfjlbList;
        //项目明细
        if(r.xmxxXmmx){
          this.cepingXmmxbList = JSON.parse(r.xmxxXmmx)
        }
         if( r.gzxxGzmx){
             //规则明细
           this.cepingGzmxbList =  JSON.parse(r.gzxxGzmx)
         }

        //被测评对象
        // this.cepingCpgzBcpmxb = res.data.data.cepingCpgzBcpmxb;
        this.cepingDjmxbList=[];
        if(r.djxxDjmx){
            //详细配置等级
           this.cepingDjmxbList =  JSON.parse(r.djxxDjmx)
        }

        //用户
        // this.cepingCpgzBcpmxbUser = res.data.data.cepingCpgzBcpmxbUser;
        //部门
        // this.cepingCpgzBcpmxbDept = res.data.data.cepingCpgzBcpmxbDept;
        this.init();

      });



    },
    init() {
      this.cepingXmmxbObj = {};
      this.zbCj = 1;
      for (var i in this.cepingXmmxbList) {
        this.cepingXmmxbObj[this.cepingXmmxbList[i].id] = this.cepingXmmxbList[i];
        if (this.zbCj < this.cepingXmmxbList[i].zbDj) {
          this.zbCj = this.cepingXmmxbList[i].zbDj;
        }
      }
      var zbList = []
      for (var i in this.cepingXmmxbList) {
        if (this.cepingXmmxbList[i].zbQz > 0) {
          let info = this.cepingXmmxbList[i];
          if (info.zbDj == 1) {
            info.zbName1 = info.zbMc;
            info.zbName2 = "";
            info.zbName3 = "";
          }
          if (info.zbDj == 2) {
            info.zbName1 = this.cepingXmmxbObj[info.pid].zbMc;
            info.zbName2 = info.zbMc;
            info.zbName3 = "";
          }
          if (info.zbDj == 3) {
            info.zbName1 = this.cepingXmmxbObj[this.cepingXmmxbObj[info.pid].pid].zbMc;
            info.zbName2 = this.cepingXmmxbObj[info.pid].zbMc;
            info.zbName3 = info.zbMc;
          }
          zbList.push(info)
        }
      }

      function compareZb(a, b) {
        return a.zbPx - b.zbPx;
      }

      var zzList = [];
      if (this.zbCj == 1) {
        zzList = zbList.sort(compareZb);
      }
      if (this.zbCj == 2) {
        var ls = [];
        for (var i in this.cepingXmmxbList) {
          if (this.cepingXmmxbList[i].pid == 0) {
            ls.push(this.cepingXmmxbList[i]);
          }
        }
        ls = ls.sort(compareZb);
        for (var j in ls) {
          var xls = []
          var qz = 0;
          for (var i in this.cepingXmmxbList) {
            if (this.cepingXmmxbList[i].pid == ls[j].id) {
              xls.push(this.cepingXmmxbList[i]);
              qz += this.cepingXmmxbList[i].zbQz;
            }
          }
          if (xls.length == 0) {
            ls[j].zbQz1 = ls[j].zbQz
            if (ls[j].zbQz > 0) {
              zzList.push(ls[j])
            }
          } else {
            var xxls = xls.sort(compareZb);
            for (var k in xxls) {
              xxls[k].zbQz1 = qz;
              if (xxls[k].zbQz > 0) {
                zzList.push(xxls[k])
              }
            }
          }
        }
      }
      if (this.zbCj == 3) {
        var ls = [];
        for (var i in this.cepingXmmxbList) {
          if (this.cepingXmmxbList[i].pid == 0) {
            ls.push(this.cepingXmmxbList[i]);
          }
        }
        ls = ls.sort(compareZb);
        for (var j in ls) {
          var xls = []
          var qz = 0;
          for (var i in this.cepingXmmxbList) {
            if (this.cepingXmmxbList[i].pid == ls[j].id) {
              xls.push(this.cepingXmmxbList[i]);
              qz += this.cepingXmmxbList[i].zbQz;
            }
          }
          if (xls.length == 0) {
            ls[j].zbQz1 = ls[j].zbQz
            if (ls[j].zbQz > 0) {
              zzList.push(ls[j])
            }
          } else {
            var xxls = xls.sort(compareZb);
            for (var k in xxls) {
              var xls1 = []
              var qz1 = 0;
              for (var ii in this.cepingXmmxbList) {
                if (this.cepingXmmxbList[ii].pid == xxls[k].id) {
                  xls1.push(this.cepingXmmxbList[ii]);
                  qz += this.cepingXmmxbList[ii].zbQz;
                  qz1 += this.cepingXmmxbList[ii].zbQz;
                }
              }
              if (xls1.length == 0) {
                xxls[k].zbQz1 = xxls[k].zbQz
                if (xxls[k].zbQz > 0) {
                  zzList.push(xxls[k])
                }
              } else {
                var xxls1 = xls1.sort(compareZb);
                for (var k1 in xxls1) {
                  xxls1[k1].zbQz1 = qz1;
                  if (xxls1[k1].zbQz > 0) {
                    zzList.push(xxls1[k1])
                  }
                }
              }
            }
          }
        }
      }
      //补充已有打分
      for (var i in zzList) {
        for (var j in this.cepingHdglXpxxDfjlbList) {
          if (this.cepingHdglXpxxDfjlbList[j].zbId == zzList[i].id) {
            zzList[i].fzId = this.cepingHdglXpxxDfjlbList[j].fzId;
            zzList[i].fzName = this.cepingHdglXpxxDfjlbList[j].fzName;
            zzList[i].fzFz = this.cepingHdglXpxxDfjlbList[j].fzFz;
            zzList[i].dfId = this.cepingHdglXpxxDfjlbList[j].id;
          }
        }
      }

      //console.log(zzList)
      this.fzbList = [];
      for (var i in zzList) {
        if (this.fzbList.indexOf(zzList[i].zbName1) == -1) {
          this.fzbList.push(zzList[i].zbName1);
        }
      }
     //console.log(this.fzbList)
      this.tableData = zzList;
      this.tableHeader = [];
      for (var i in this.cepingGzmxbList) {
        if(this.cepingGzmxbList[i].fzSsz){
          var find=this.tableHeader.find(item=>item==this.cepingGzmxbList[i].fzSsz);
          if(!find){
            this.tableHeader.push(this.cepingGzmxbList[i].fzSsz);
          }
        }
        // if(this.cepingGzmxbList[i].fzSsz){
        //   this.tableHeader.push(this.cepingGzmxbList[i].fzSsz);
        // }
      }
      // for (var i in this.cepingGzmxbList) {
      //   if (this.cepingGzmxbList[i].fzSsz == "优秀") {
      //     this.tableHeader.push("优秀");
      //     break;
      //   }
      // }
      // for (var i in this.cepingGzmxbList) {
      //   if (this.cepingGzmxbList[i].fzSsz == "较好") {
      //     this.tableHeader.push("较好");
      //     break;
      //   }
      // }
      // for (var i in this.cepingGzmxbList) {
      //   if (this.cepingGzmxbList[i].fzSsz == "一般") {
      //     this.tableHeader.push("一般");
      //     break;
      //   }
      // }
      // for (var i in this.cepingGzmxbList) {
      //   if (this.cepingGzmxbList[i].fzSsz == "较差") {
      //     this.tableHeader.push("较差");
      //     break;
      //   }
      // }

      function compareGz(a, b) {
        return a.fzPx - b.fzPx;
      }

      this.tableColumn = this.cepingGzmxbList.sort(compareGz);
      this.cepingDjmxb = {};
      if (this.cepingHdglXpxxDfjlbList != null & this.cepingHdglXpxxDfjlbList.length > 0) {
        this.cepingHdglXpxxDfjlbList.forEach(r => {
          if (r.dfLx == 1) {
            this.cepingDjmxb = r;
          }
        })
      }

      this.show = true;

    },
    objectSpanMethod({row, column, rowIndex, columnIndex}) {
      if (this.zbCj == 1) {
        return {
          rowspan: 1,
          colspan: 1
        };
      } else {
        if (columnIndex === 0) {
          if (rowIndex > 0 && this.tableData[rowIndex].zbName1 == this.tableData[rowIndex - 1].zbName1) {
            return {
              rowspan: 0,
              colspan: 1
            }
          } else {
            var j = 0;
            for (var i = rowIndex; i < this.tableData.length; i++) {
              if (this.tableData[rowIndex].zbName1 == this.tableData[i].zbName1) {
                j++;
              } else {
                break;
              }
            }
            return {
              rowspan: j,
              colspan: 1
            }
          }
        }
        if (this.zbCj == 3 && columnIndex === 1) {
          if (rowIndex > 0 && this.tableData[rowIndex].zbName2 == this.tableData[rowIndex - 1].zbName2 && this.tableData[rowIndex].zbName1 == this.tableData[rowIndex - 1].zbName1) {
            return {
              rowspan: 0,
              colspan: 1
            }
          } else {
            var j = 0;
            for (var i = rowIndex; i < this.tableData.length; i++) {
              if (this.tableData[rowIndex].zbName2 == this.tableData[i].zbName2 && this.tableData[rowIndex].zbName1 == this.tableData[i].zbName1) {
                j++;
              } else {
                break;
              }
            }
            return {
              rowspan: j,
              colspan: 1
            }
          }
        }
      }
    },

    getClass(i, len) {
      let x = i + 1;
      if (x / len <= 0.25) {
        return "pjyx";
      } else if (x / len <= 0.5) {
        return "pjjh";
      } else if (x / len <= 0.75) {
        return "pjyb";
      } else {
        return "pjjc";
      }
    },
      tableRowClassName({ row, rowIndex }) {
            let fz = this.fzbList.indexOf(this.tableData[rowIndex].zbName1);
            if (fz % 2 == 0) {
              return 'cpRowNormal';
            } else {
              return 'cpRowstripe';
            }
            return '';
          }
        }
}
</script>

<style scoped>
</style>
<style>
.newline{
  white-space:pre-line!important;
}
</style>

